package com.uchengwang.ui.cm_ui.umsg.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.UniConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ActivityTitleBar;
import com.uchengwang.ui.base_ui.ui.AllBaseActivity;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.UCommonUtil;


/**
 * Created by U城网 on 2017/9/12.
 * uni提交社团信息
 */

public class SubmitClubActivity extends AllBaseActivity implements View.OnClickListener{
    private ActivityTitleBar submit_titleBar;
    private TextView submit_school,  submit_clubType;
    private EditText submit_name, submit_admin;
    private Button submit_ok;

    private String orgType, clubName, clubAdmin;//社团类型

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_submit_club);
        initView();
    }

    private void initView(){
        submit_titleBar = (ActivityTitleBar) findViewById(R.id.submit_titleBar);
        submit_titleBar.setTitle("提交社团", 0);

        submit_school = (TextView) findViewById(R.id.submit_school);
        submit_clubType = (TextView) findViewById(R.id.submit_clubType);
        submit_clubType.setOnClickListener(this);
        submit_name = (EditText) findViewById(R.id.submit_name);
        submit_admin = (EditText) findViewById(R.id.submit_admin);
        submit_ok = (Button) findViewById(R.id.submit_ok);
        submit_ok.setOnClickListener(this);

        initData();

    }

    private void initData(){
        submit_school.setText(UChengApplication.mySchoolName);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.submit_clubType:
                startActivityForResult(new Intent(this, ClubTypeActivity.class), 0);
                break;
            case R.id.submit_ok:
                clubName = submit_name.getText().toString();
                clubAdmin = submit_admin.getText().toString();
                if (TextUtils.isEmpty(clubName)){
                    UCommonUtil.getUtil().showToast(this, "请输入社团名称");
                    return;
                }
                if (TextUtils.isEmpty(orgType)){
                    UCommonUtil.getUtil().showToast(this, "请选择社团类型");
                    return;
                }

                if (TextUtils.isEmpty(clubAdmin)){
                    UCommonUtil.getUtil().showToast(this, "请输入社长名");
                    return;
                }

                LoadingUtils.getUtils().loading(this);
//                int type = GetInfoUtils.getUtils().getClubType(orgType);
                MapRequest map = new MapRequest();
                map.put(BaseConfig.UNI_ID, UChengApplication.myUniId);
                map.put(BaseConfig.UNI_NAME, UChengApplication.mySchoolName);
                map.put(BaseConfig.ORG_NAME, clubName);
//                map.put(BaseConfig.ORG_TYPE, type);
                map.put(BaseConfig.ORG_ADMIN_NAME, clubAdmin);
                map.put(BaseConfig.UNI_NICK_NAME, UChengApplication.nickName);
                map.put(BaseConfig.UNI_UID, UChengApplication.uid);

                HttpManager.newInstance(this).query(UniConfig.UniAuthenClub.method, UniConfig.UniAuthenClub.URL, map, new HttpCallBack() {
                    @Override
                    public void onError(int status, String msg) {
                        Log.d("jjf", "stutas="+status+"=msg="+msg);
                        LoadingUtils.getUtils().hide();
                        UCommonUtil.getUtil().showToast(SubmitClubActivity.this, "提交失败");

                    }

                    @Override
                    public void onResp(int status, String msg, String data) {
                        Log.d("jjf", "stutas="+status+"=msg="+msg);
                        LoadingUtils.getUtils().hide();
                        if (status ==0){
                            UCommonUtil.getUtil().showToast(SubmitClubActivity.this, "提交成功, 感谢您的反馈!");
                            SubmitClubActivity.this.finish();
                        }else {
                            UCommonUtil.getUtil().showToast(SubmitClubActivity.this, msg);
                        }
                    }
                });

                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode ==0 && resultCode ==1 && data != null){
                    orgType = data.getStringExtra(BaseConfig.TYPE);
                    submit_clubType.setText(orgType);
            }
        }
}
